package com.wzh.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * @author xiaoming
 * @Description: 校验规则的校验器
 */
public class ListValueConsteaintValidator implements ConstraintValidator<ListValue,Integer> {

    private Set<Integer> set=new HashSet<>();
    //初始化
    //ListValue是指定的校验注解
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int vals[]=constraintAnnotation.vals();
        if (vals.length>0){
            for (int val : vals){
                set.add(val);
            }
        }
    }

    //校验是否成功
    //integer是需要校验的值
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
        return set.contains(integer);
    }
}
